package com.lwj.liwanjia.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lwj.liwanjia.R;
import com.lwj.liwanjia.bean.SeckillTimeBean;
import com.lwj.liwanjia.config.OnItemClickListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/9/11.
 */

public class SeckillTimeAdapter extends RecyclerView.Adapter<SeckillTimeAdapter.MyViewHolder> {
    private Context mContext;
    private List<SeckillTimeBean.DataBean.TimeSlotBean> list;
    private LayoutInflater inflater;
    private String now;
    private OnItemClickListener listener;
    private int clickPosition = -1;

    public void setClickPosition(int clickPosition) {
        this.clickPosition = clickPosition;
    }

    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public void setNow(String now) {
        this.now = now;
    }

    public SeckillTimeAdapter(Context mContext, List<SeckillTimeBean.DataBean.TimeSlotBean> list) {
        this.mContext = mContext;
        this.list = list;
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.seckill_time_item, parent, false);
        MyViewHolder holder1 = new MyViewHolder(view);
        return holder1;
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        holder.tvTime.setText(list.get(position).getSeckillStartTime());
        holder.tvState.setText(list.get(position).getType());
        switch (list.get(position).getType()) {
            case "已结束":
                holder.tvTime.setTextColor(mContext.getResources().getColor(R.color.colorCECECE));
                holder.tvState.setTextColor(mContext.getResources().getColor(R.color.colorCECECE));
                break;
            case "抢购中":
                holder.tvTime.setTextColor(mContext.getResources().getColor(R.color.colorFF3B15));
                holder.tvState.setTextColor(mContext.getResources().getColor(R.color.colorFF3B15));
                break;
            case "即将开始":
                holder.tvTime.setTextColor(mContext.getResources().getColor(R.color.color3B3B3B));
                holder.tvState.setTextColor(mContext.getResources().getColor(R.color.color3B3B3B));
                break;
        }
        if (clickPosition == position) {
            if (!list.get(position).getType().equals("抢购中")) {
                holder.tvTime.setTextColor(mContext.getResources().getColor(R.color.colorC4C4C4));
                holder.tvState.setTextColor(mContext.getResources().getColor(R.color.colorC4C4C4));
            }
        }

        if (listener != null) {
            holder.timeLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!list.get(position).getType().equals("已结束")) {
                        listener.OnItemClick(v, position);
                    } else {
                        Toast.makeText(mContext, "活动已结束", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }


    class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.tv_state)
        TextView tvState;
        @BindView(R.id.time_layout)
        LinearLayout timeLayout;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
